package com.equdao.car_violation_inquiry.utils;

import com.equdao.car_violation_inquiry.pojo.BaseCarDO;
import com.equdao.car_violation_inquiry.pojo.BasicCarDO;
import com.equdao.car_violation_inquiry.service.LocalBaseCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 查道强 on 2018/1/8.
 */
@RestController
public class UploadLocalCsvUtils {

    @Autowired
    LocalBaseCarService localBaseCarService;

    @GetMapping("/uploadLocalCsv")
    public void uploadLocalCsv() {

        DataInputStream in = null;
        BufferedReader br = null;
        try {
            in = new DataInputStream(new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\宁波.csv")));
            br = new BufferedReader(new InputStreamReader(in, "GBK"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        }
        String line = "";
        String everyLine = "";
        ArrayList<BaseCarDO> basicCarDOS = new ArrayList<>();
        try {
            int i = 0;
            while ((line = br.readLine()) != null)  //读取到的内容给line变量
            {
                everyLine = line;
                //System.out.println(everyLine);
                if (i>0){
                    everyLine = line;
                    //System.out.println(everyLine);
                    String[] split = everyLine.split(",");
                    BaseCarDO baseCarDO = new BaseCarDO();
                    baseCarDO.setVehicleNo(split[0]);//添加
                    baseCarDO.setFrameNo(split[1]);
                    baseCarDO.setEngineNo(split[2]);
                    basicCarDOS.add(baseCarDO);
                }else {
                    i++;
                }
            }


            System.out.println("csv表格中所有行数：" + basicCarDOS.size());
            //将数据批量写入数据库中
            int average = basicCarDOS.size() / 1000;
            System.out.println("总数" + basicCarDOS.size());

            System.out.println("总共" + average +"批数据");
            int remainder = basicCarDOS.size() % 1000;
            System.out.println("余数" + remainder);

            for (int j = 0; j < average +1  ; j++) {
                if (j < average){
                    List<BaseCarDO> baseCarDOS =  basicCarDOS.subList(1000 * j, 1000 * (j + 1));
                    Integer integer = localBaseCarService.saveBaseCars(baseCarDOS);
                }else {
                    List<BaseCarDO> baseCarDOS =  basicCarDOS.subList(1000 * j, basicCarDOS.size());
                Integer integer = localBaseCarService.saveBaseCars(baseCarDOS);
                }
                System.out.println("写入了" + (j+1) + "次");
            }




        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
